<template>
  <div class="member-grade-page">
    <ElCard shadow="never">
      <template #header>
        <div class="card-header">
          <span>会员级别</span>
          <ElButton type="primary" @click="onCreate">新增级别</ElButton>
        </div>
      </template>

      <ElTable :data="tableData" v-loading="loading" border>
        <ElTableColumn prop="id" label="ID" width="80" align="center" />
        <ElTableColumn prop="name" label="级别名称" min-width="160" />
        <ElTableColumn prop="sort" label="排序" width="100" align="center" />
        <ElTableColumn label="状态" width="120" align="center">
          <template #default="{ row }">
            <ElSwitch :model-value="row.status === 1" @change="(v:boolean)=>onStatus(row,v)" />
          </template>
        </ElTableColumn>
        <ElTableColumn label="操作" width="220" align="center" fixed="right">
          <template #default="{ row }">
            <ElButton size="small" @click="onEdit(row)">编辑</ElButton>
            <ElPopconfirm title="确认删除该级别？" @confirm="() => onDelete(row)">
              <template #reference>
                <ElButton size="small" type="danger">删除</ElButton>
              </template>
            </ElPopconfirm>
          </template>
        </ElTableColumn>
      </ElTable>

      <div class="table-footer">
        <ElPagination
          background
          layout="prev, pager, next, jumper, ->, total"
          :total="total"
          :page-size="query.pageSize"
          :current-page="query.page"
          @current-change="onPageChange"
        />
      </div>
    </ElCard>

    <ElDialog v-model="dialog.visible" :title="dialog.title" width="520px">
      <ElForm :model="form" :rules="rules" ref="formRef" label-width="88px">
        <ElFormItem label="级别名称" prop="name">
          <ElInput v-model="form.name" placeholder="请输入级别名称" />
        </ElFormItem>
        <ElFormItem label="排序" prop="sort">
          <ElInputNumber v-model="form.sort" :min="0" :max="9999" />
        </ElFormItem>
      </ElForm>
      <template #footer>
        <ElButton @click="dialog.visible=false">取 消</ElButton>
        <ElButton type="primary" :loading="submitLoading" @click="onSubmit">确 定</ElButton>
      </template>
    </ElDialog>
  </div>
  
</template>

<script setup lang="ts">
import { onMounted, reactive, ref } from 'vue'
import { ElMessage, ElMessageBox, FormInstance, FormRules } from 'element-plus'
import { memberService } from '@/api/memberApi'

interface GradeItem {
  id: number
  name: string
  sort: number
  status: number
}

const loading = ref(false)
const submitLoading = ref(false)
const tableData = ref<GradeItem[]>([])
const total = ref(0)
const query = reactive({ page: 1, pageSize: 10 })

const dialog = reactive({ visible: false, title: '新增级别' })
const formRef = ref<FormInstance>()
const form = reactive<Partial<GradeItem>>({ id: undefined, name: '', sort: 0 })

const rules: FormRules = {
  name: [{ required: true, message: '请输入级别名称', trigger: 'blur' }]
}

async function fetchList() {
  loading.value = true
  try {
    const res: any = await memberService.memberGradeList({ page: query.page, pageSize: query.pageSize })
    tableData.value = (res?.data?.list ?? res?.data ?? []) as GradeItem[]
    total.value = Number(res?.data?.total ?? tableData.value.length)
  } finally {
    loading.value = false
  }
}

function onCreate() {
  dialog.visible = true
  dialog.title = '新增级别'
  Object.assign(form, { id: undefined, name: '', sort: 0 })
}

function onEdit(row: GradeItem) {
  dialog.visible = true
  dialog.title = '编辑级别'
  Object.assign(form, row)
}

async function onSubmit() {
  if (!formRef.value) return
  await formRef.value.validate()
  submitLoading.value = true
  try {
    if (form.id) {
      await memberService.memberGradeEdit(form as any)
      ElMessage.success('修改成功')
    } else {
      await memberService.memberGradeCreate(form as any)
      ElMessage.success('新增成功')
    }
    dialog.visible = false
    fetchList()
  } finally {
    submitLoading.value = false
  }
}

async function onDelete(row: GradeItem) {
  await memberService.memberGradeDelete({ id: row.id })
  ElMessage.success('删除成功')
  fetchList()
}

async function onStatus(row: GradeItem, v: boolean) {
  await memberService.memberGradeStatus({ id: row.id, status: v ? 1 : 0 })
  ElMessage.success('状态已更新')
  fetchList()
}

function onPageChange(p: number) {
  query.page = p
  fetchList()
}

onMounted(fetchList)
</script>

<style scoped>
.member-grade-page .card-header{
  display:flex;align-items:center;justify-content:space-between;
}
.table-footer{padding:12px 0;display:flex;justify-content:flex-end;}
</style>


